package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsh.erp.datasource.entities.Examine;
import com.jsh.erp.service.examine.ExamineService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.StringUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author gonge
 */
@RestController
@RequestMapping("/examine")
public class ExamineController {

    @Resource
    private ExamineService examineService;

    @PostMapping("/created")
    public BaseResponseInfo createExamine(@RequestBody JSONObject obj, HttpServletRequest request) throws Exception {
        BaseResponseInfo info = new BaseResponseInfo();
        examineService.createExamine(obj,request);
        info.code = 200;
        return info;
    }

    @PutMapping("/update")
    public String updateExamine(@RequestBody Examine Examine) {
        examineService.updateById(Examine);
        return "质检记录更新成功";
    }

    @GetMapping("/{id}")
    public Examine getExamineById(@PathVariable Long id) {
        return examineService.getById(id);
    }

    /**
     * 查询采购质检ListData
     * @param search
     * @param returnReason
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/all")
    public BaseResponseInfo getAllExamine(@RequestParam(value = "search",defaultValue = "") String search,
                                          @RequestParam(value = "returnReason",defaultValue = "") String returnReason,
                                          @RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",defaultValue = "1000000") Integer pageSize) {
        Page<Examine> page = new Page<>(currentPage,pageSize);
        // 构建查询条件
        QueryWrapper<Examine> queryWrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(returnReason)){
            queryWrapper.eq("return_reason",returnReason);
        }
        queryWrapper.orderByDesc("created_at");

        IPage<Examine> examineIPage = examineService.page(page,queryWrapper);
        JSONObject val = new JSONObject();
        val.put("rows",examineIPage.getRecords());
        val.put("total",examineIPage.getTotal());

        BaseResponseInfo info = new BaseResponseInfo();
        info.code =200;
        info.data = val;
        return info;
    }
    @GetMapping("/byExamineCode")
    public List<Examine> getExamineByExamineCode(@RequestParam Long examineCode) {
        return examineService.list(
            new QueryWrapper<Examine>().eq("examine_code", examineCode)
        );
    }

    /**
     * 根据销售订号查询对应的质检单据。
     * @param defaultNumber
     * @return
     */
    @GetMapping("/byPurchaseNumber")
    public List<Examine> getExamineByPurchaseNumber(@RequestParam(value = "defaultNumber") String defaultNumber) {
        return examineService.list(
                new QueryWrapper<Examine>().eq("purchase_order_number", defaultNumber)
        );
    }
}